var url = {
	role_list: api.role.list,
	add: api.user.add,
	edit: api.user.edit,
	list:  api.user.list,
}
var loadcheck=1;
{if $Think.get.id}
var loadarr=[0,0]
{else/}
var loadarr=[0]
{/if}
Load()
var role_id= '{$Think.get.role_id}';
ajax(url.role_list, {page:1,pagenum:1000}, 'post').done(function(res) {
	loadcheck=1;
	loadarr[0]=1
	for(var lidx=0;lidx<loadarr.length;lidx++){
		if(loadarr[lidx]){
			
		}else{
			loadcheck=0
		}
	}
	if(loadcheck){
		Load(1)
	}
	var options=[];
	for(var i=0;i<res.list.length;i++){
		if(res.list[i].role_id!=5&&res.list[i].role_id!=2){
			options.push({
			name:res.list[i].role_name,
			value:res.list[i].role_id
		})
		}
	}
	console.log(options)
	$('#role_id').html(options)
	formSelects.data('select1', 'local', {
	    arr:options
	});
	if(role_id){
		formSelects.value('select1',role_id.split()); 
		formSelects.disabled('select1');
	}
	


}).fail(function(err) {
	Err('请求失败')
});
{if $Think.get.id}
	ajax(url.list,{
		where_json:JSON.stringify({
			user_id:['in',"{$Think.get.id}"]
		})
	},'post').done(function(res) {
		loadcheck=1;
		loadarr[1]=1
		for(var lidx=0;lidx<loadarr.length;lidx++){
			if(loadarr[lidx]){
				
			}else{
				loadcheck=0
			}
		}
		if(loadcheck){
			Load(1)
		}
		console.log(res)
		for(var key in res.list[0]){
			if(key=='role_id'){
				role_id_arr = res.list[0].role_id.split(",");
				formSelects.value('select1', role_id_arr);
			}else{
				$('input[name='+key+']').val(res.list[0][key])
			}
		}
	}).fail(function(err) {
		Err('请求失败') 
	});
{/if}
$('.bacaklist').on('click',function(){
	window.location.href='/html/system/user.html?op=search'
})
form.on('submit(ss1)', function(data) {
	Load()
	var sumbit;
	{if $Think.get.id}
	sumbit=url.edit
	data.field.user_id = '{$Think.get.id}';
	{else/}
	sumbit=url.add
	{/if}
	//当前容器的全部表单字段，名值对形式：{name: value}
	data.field.module_type = 0;
	console.log(1)
	console.log(data.field);
	data.field.state=1;
	data.field.company_id=0;
	{if $Think.get.role_id}
	data.field.role_id='{$Think.get.role_id}';
	{/if}
	data.field.username = data.field.mobile;
	ajax(sumbit, data.field, 'post').done(function(res) {
		Load(1)
		if(res.code==1){
			Msg('添加成功')
			{if $Think.get.id}
			window.history.go(0)
			{else/}
				{if $Think.get.op}
				window.location.href = '{:url("/html/system/user")}?op=search'
				{else/}
				window.location.href = '{:url("/html/system/user")}'
				{/if}
			{/if}
		}else{
			Err(res.msg)
		}
		console.log(res)
	}).fail(function(err) {
		Load(1)
		Err('请求失败')
	});

	return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
});